package com.sky.service.impl;

import com.sky.dto.BusinessOrderData;
import com.sky.dto.UserDataStatistic;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;


//    private Double turnover;//营业额
//    private Integer validOrderCount;//有效订单数
//    private Double orderCompletionRate;//订单完成率
//    private Double unitPrice;//平均客单价
//    private Integer newUsers;//新增用户数
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        List<UserDataStatistic> newUserEveryDay = userMapper.getNewUserEveryDay(begin.toLocalDate(), end.toLocalDate());
        Integer newUserCount = 0;
        if(newUserEveryDay != null&&newUserEveryDay.size()>0) {
            newUserCount = newUserEveryDay.get(0).getNewUsers();
        }

//        BusinessOrderData orderData = orderMapper.getBusinessOrderData(begin,end);
        List<BusinessOrderData> businessOrderData = orderMapper.getBusinessOrderDataIsEveryDay(begin, end, 0);
        BusinessOrderData orderData = new BusinessOrderData(null,0.0,0,0);;
//        if(orderData==null) {
//            orderData=new BusinessOrderData(null,0.0,0,0);
//        }
        if(businessOrderData!=null&& !businessOrderData.isEmpty()) {
            orderData=businessOrderData.get(0);
        }


        Double rate=0.0;
        if(orderData.getTotalOrders()!=0) {
            rate= orderData.getValidOrderCount().doubleValue()/orderData.getTotalOrders();
        }
        Double unitPrice=0.0;
        if(orderData.getValidOrderCount()!=0){
            unitPrice=orderData.getTurnover()/orderData.getValidOrderCount();
        }

        return BusinessDataVO.builder()
                .turnover(orderData.getTurnover()==null?0.0:orderData.getTurnover())
                .validOrderCount(orderData.getValidOrderCount())
                .orderCompletionRate(rate)
                .unitPrice(unitPrice)
                .newUsers(newUserCount)
                .build();
    }

//    // 已启售数量
//    private Integer sold;
//    // 已停售数量
//    private Integer discontinued;
    public SetmealOverViewVO getOverviewSetmeals(LocalDateTime begin, LocalDateTime end) {
        return setMealMapper.getOverviewSetmeals(begin,end);
    }

//   // 已启售数量
//    private Integer sold;
//    // 已停售数量
//    private Integer discontinued;
    public DishOverViewVO getOverviewDishes(LocalDateTime begin, LocalDateTime end) {

        return dishMapper.getOverviewDishes(begin,end);
    }



//    //待接单数量
//    private Integer waitingOrders;
//    //待派送数量
//    private Integer deliveredOrders;
//    //已完成数量
//    private Integer completedOrders;
//    //已取消数量
//    private Integer cancelledOrders;
//    //全部订单
//    private Integer allOrders;
    public OrderOverViewVO getOverviewOrders(LocalDateTime begin, LocalDateTime end) {


        return orderMapper.getOverviewOrders(begin,end);

    }
}
